package com.laxcen.basiclib.core.update.custom;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laxcen.basiclib.core.update.Update;
import com.laxcen.basiclib.core.update.entity.ApiResult;
import com.laxcen.basiclib.core.update.entity.AppVersionInfo;
import com.laxcen.basiclib.core.update.entity.UpdateEntity;
import com.laxcen.basiclib.core.update.proxy.impl.AbstractUpdateParser;
import com.laxcen.basiclib.core.update.proxy.impl.DefaultUpdateParser;
import com.laxcen.basiclib.core.update.utils.UpdateUtils;
import com.laxcen.basiclib.engine.ApiEngine;
import com.laxcen.basiclib.utils.JsonUtil;
import com.laxcen.basiclib.utils.LogUtil;


/**
 *  更换后台数据 判断用哪种方式更新
 */
public class UpdateServiceParser extends AbstractUpdateParser {
    @Override
    public UpdateEntity parseJson(String json){
        ApiResult<AppVersionInfo> apiResult = JSON.parseObject(json,new TypeReference<ApiResult<AppVersionInfo>>(){});
        if (apiResult != null) {
            AppVersionInfo appVersionInfo = doLocalCompare(apiResult.getData());

            UpdateEntity updateEntity = new UpdateEntity();
            //无更新
            if (appVersionInfo.getUpdateStatus() == DefaultUpdateParser.APIConstant.NO_NEW_VERSION) {
                updateEntity.setHasUpdate(false);
                LogUtil.i("update","无需更新");
            } else {
                if (appVersionInfo.getUpdateStatus() == DefaultUpdateParser.APIConstant.HAVE_NEW_VERSION_FORCED_UPLOAD) {
                    updateEntity.setForce(true);
                }else if(appVersionInfo.getUpdateStatus()==DefaultUpdateParser.APIConstant.HAVE_NEW_VERSION_BACKGROUND){
                    updateEntity.setIsSilent(true);
                }else if(appVersionInfo.getUpdateStatus()==DefaultUpdateParser.APIConstant.HAVE_NEW_VERSION_AUTO){
                    updateEntity.setIsAutoMode(true);
                }else if(appVersionInfo.getUpdateStatus()==DefaultUpdateParser.APIConstant.HAVE_NEW_VERSION){
                    //可忽略
                    updateEntity.setIsIgnorable(true);
                }
                updateEntity.setHasUpdate(true)
                        //兼容一下
                        .setUpdateContent(appVersionInfo.getModifyContent().replaceAll("\\\\r\\\\n", "\r\n"))
                        .setVersionCode(appVersionInfo.getVersionCode())
                        .setVersionName(appVersionInfo.getVersionName())
                        .setDownloadUrl(appVersionInfo.getDownloadUrl())
                        .setSize(appVersionInfo.getApkSize())
                        .setMd5(appVersionInfo.getApkMd5())
                        .setH5(appVersionInfo.isH5());
            }
            LogUtil.e("updateEntity",JsonUtil.beanToJson(updateEntity));
            return updateEntity;
        }
        return null;
    }



    /**
     * 进行本地版本判断[防止服务端出错，本来是不需要更新，但是服务端返回是需要更新]
     *
     * @param appVersionInfo
     * @return
     */
    private AppVersionInfo doLocalCompare(AppVersionInfo appVersionInfo) {
        //服务端返回需要更新
        if (appVersionInfo.getUpdateStatus() != DefaultUpdateParser.APIConstant.NO_NEW_VERSION) {
            int lastVersionCode = appVersionInfo.getVersionCode();

            //判断是不是H5更新
            if(appVersionInfo.isH5()){
                //如果存在H5版本，检查H5版本是否需要更新
                if(ApiEngine.h5AppConfig!=null&&ApiEngine.h5AppConfig.getVersionCode()!=0){
                    if(lastVersionCode<=ApiEngine.h5AppConfig.getVersionCode()){
                        appVersionInfo.setUpdateStatus(DefaultUpdateParser.APIConstant.NO_NEW_VERSION);
                    }
                }
            }else{
                //最新版本小于等于现在的版本，不需要更新
                if (lastVersionCode <= UpdateUtils.getVersionCode(Update.getContext())) {
                    appVersionInfo.setUpdateStatus(DefaultUpdateParser.APIConstant.NO_NEW_VERSION);
                }
            }
        }
        return appVersionInfo;
    }
}
